package com.bosssoft.boss_exam_web.Controller;

import com.bosssoft.boss_exam_core.common.config.security.AdminJwtTokenHelper;
import com.bosssoft.boss_exam_core.common.config.security.ApplicantJwtTokenHelper;
import com.bosssoft.boss_exam_core.exception.BusinessException;
import com.bosssoft.boss_exam_core.util.security.bcrypt.BCryptPasswordEncoder;
import com.bosssoft.boss_exam_core.util.web.ResponseUtil;
import com.bosssoft.boss_exam_db.entity.Admin;
import com.bosssoft.boss_exam_db.entity.Applicant;
import com.bosssoft.boss_exam_db.entity.Role;
import com.bosssoft.boss_exam_db.service.busines.AdminService;
import com.bosssoft.boss_exam_db.service.busines.ApplicantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.Size;
import java.util.List;

@RestController
@Slf4j
@Validated
@Api(value="登入控制器")
public class AuthController {

//    注入jwtTokenHelper对象
    @Autowired
    private AdminJwtTokenHelper adminJwtTokenHelper;

//    注入应聘人JwtToken操作服务
    @Autowired
    private ApplicantJwtTokenHelper applicantJwtTokenHelper;

//   注入管理员服务
    @Autowired
    private AdminService adminService;

//   注入应聘人服务
    @Autowired
    private ApplicantService applicantService;


    @ApiOperation(value = "后台管理员登入", notes = "后台管理员可根据账户密码登入", httpMethod = "GET", response = Object.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", dataType = "String", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "Password", value = "密码", dataType = "String", required = true, dataTypeClass = String.class)
    })
    @RequestMapping("/admin/auth/login")
    public Object AdminLogin(@Size(min = 1,max = 10) @RequestParam(required = true) String username, @RequestParam(required = true)String password) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encode = encoder.encode(password);
        System.out.println("pas" + encode);
        Example example = new Example(Admin.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username", username)
                .andEqualTo("deleted", false);
        List<Admin> adminList = adminService.selectByExample(example);
        if (adminList.size() == 0) {
            return ResponseUtil.badArgumentValue();
        }
        Admin admin = adminList.get(0);
        List<Role> roles = adminService.queryRolesById(admin.getId());
        if (roles.size() == 0 || roles == null) {
            throw new BusinessException("当前用户没有角色，有问题");
        }
        admin.setRoles(roles);
        String tokenResult = adminJwtTokenHelper.create(admin);
        System.out.println("创建的token" + tokenResult);
        Admin parse = adminJwtTokenHelper.parse(tokenResult);
        System.out.println("重新解析出来的adminlys对象" + parse);
        // 将jwt生成的token发送给客户端
        Object result = ResponseUtil.ok("登录成功!", tokenResult);
            return result;
    }

    @ApiOperation(value = "手机端应聘人登入", notes = "手机端，应聘人可根据账户密码登入", httpMethod = "POST", response = Object.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", dataType = "String", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "Password", value = "密码", dataType = "String", required = true, dataTypeClass = String.class)
    })
    @PostMapping("/phone/auth/login")
    public Object applicantPhoneLogin(@Size(min = 1,max = 10) @RequestParam(required = true) String username,@RequestParam(required = true) String password) {
        Example example = new Example(Applicant.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username", username)
                .andEqualTo("deleted", false);
        List<Applicant> applicantList = applicantService.selectByExample(example);
        if (applicantList.size() == 0) {
            return ResponseUtil.badArgumentValue();
        }
        Applicant applicant = applicantList.get(0);
        String tokenResult = applicantJwtTokenHelper.create(applicant);
        System.out.println("创建的token" + tokenResult);
        Applicant parse = applicantJwtTokenHelper.parse(tokenResult);
        System.out.println("重新解析出来的adminlys对象" + parse);
        // 将jwt生成的token发送给客户端
        Object result = ResponseUtil.ok("登录成功!", tokenResult);
        return result;
    }

    @ApiOperation(value = "PC端应聘人登入", notes = "PC端，应聘人可根据账户密码登入", httpMethod = "POST", response = Object.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", dataType = "String", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "Password", value = "密码", dataType = "String", required = true, dataTypeClass = String.class)
    })
    @PostMapping("/pc/auth/login")
    public Object applicantPCLogin(@Size(min = 1,max = 10) @RequestParam(required = true) String username,@RequestParam(required = true) String password) {
        Example example = new Example(Applicant.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username", username)
                .andEqualTo("deleted", false);
        List<Applicant> applicantList = applicantService.selectByExample(example);
        if (applicantList.size() == 0) {
            return ResponseUtil.badArgumentValue();
        }
        Applicant applicant = applicantList.get(0);
        String tokenResult = applicantJwtTokenHelper.create(applicant);
        System.out.println("创建的token" + tokenResult);
        Applicant parse = applicantJwtTokenHelper.parse(tokenResult);
        System.out.println("重新解析出来的adminlys对象" + parse);
        // 将jwt生成的token发送给客户端
        Object result = ResponseUtil.ok("登录成功!", tokenResult);
        return result;
    }

}
